package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.vos.UserInfo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.user.mapper.UserMapper;
import com.heima.model.user.pojos.User;
import com.heima.user.service.IUserService;
import com.heima.utils.common.AppJwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static com.heima.model.common.enums.RedisConstant.LOGIN_USER_TOKEN;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    private final StringRedisTemplate redisTemplate;

    /**
     * 用户登录
     *
     * @param loginDto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto loginDto) {
        //1.验证手机号或密码是否存在
        if (loginDto == null || StringUtils.isEmpty(loginDto.getPhone()) || StringUtils.isEmpty(loginDto.getPassword())) {
            return ResponseResult.errorResult(201, "请输入账号或密码");
        }
        //2.验证手机号格式是否正确 省略
        //3.查看该手机号用户是否存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", loginDto.getPhone());
        User user = getOne(queryWrapper);
        if (user == null) {
            return ResponseResult.errorResult(201, "手机号或账号密码错误");
        }
        //4.验证密码是否正确
        String salt = user.getSalt();
        String password = loginDto.getPassword() + salt;
        String pw = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!user.getPassword().equals(pw)) {
            return ResponseResult.errorResult(201, "手机号或账号密码错误");
        }
        if (user.getStatus() == 0) {
            return ResponseResult.errorResult(201, "该用户已被禁用");
        }
        //5.生成token
        String token = AppJwtUtil.getToken(user.getId());
        UserInfo userInfo = new UserInfo();
        userInfo.setToken(token);
        return ResponseResult.okResult(userInfo);
    }

    /**
     * 基于redis+token的登录
     *
     * @param loginDto
     * @return
     */
    @Override
    public ResponseResult loginByRedisWithToken(LoginDto loginDto) {
        //1.验证手机号和密码是否存在
        if (loginDto == null || StringUtils.isEmpty(loginDto.getPhone()) || StringUtils.isEmpty(loginDto.getPassword())) {
            return ResponseResult.errorResult(201, "请输入账号或密码");
        }
        //2.验证手机号格式是否正确 省略
        //3.查看该手机号用户是否存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", loginDto.getPhone());
        User user = getOne(queryWrapper);
        if (user == null) {
            return ResponseResult.errorResult(201, "手机号或账号密码错误");
        }
        //4.查看账号是否被禁用
        if (user.getStatus() == 0) {
            return ResponseResult.errorResult(201, "该用户已被禁用");
        }
        //5.验证密码是否正确
        String salt = user.getSalt();
        String password = loginDto.getPassword() + salt;
        String pw = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!user.getPassword().equals(pw)){
            return ResponseResult.errorResult(201, "手机号或账号密码错误");
        }
        //6.生成token
        String token = UUID.randomUUID().toString();
        //7.存入redis
        redisTemplate.opsForValue().set( LOGIN_USER_TOKEN + token, user.getId().toString(), 60, TimeUnit.MINUTES);
        UserInfo userInfo = new UserInfo();
        userInfo.setToken(token);
        return ResponseResult.okResult(userInfo);
    }
}
